package com.greendizer.api.test.snippets;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.junit.Test;

import com.greendizer.api.client.SellerClient;
import com.greendizer.api.dal.Collection;
import com.greendizer.api.resource.seller.Email;
import com.greendizer.api.resource.seller.Invoice;
import com.greendizer.api.resource.seller.Seller;


public class ThirdSnippet {

  @Test
  public void SnippetTest() {
    String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
    String query = String.format("canceled==false|paid==false|duedate<<%s", today);

    Seller seller = new SellerClient("oauth_token").getUser();
    Email email = seller.getEmails().getById("example@domain.com");
    Collection<Invoice> invoices = email.getInvoices().search(query);
    invoices.populate();

    for (Invoice invoice : invoices) {
      invoice.setFlagged(true);
      invoice.save();
      System.out.println(String.format("%s (%s %s)", invoice.getName(), invoice.getTotal(), invoice.getCurrency()));
      System.out.println(String.format("https://viewer.greendizer.com/#%s", invoice.getSecretKey()));
    }
  }
}
